/*
 * @copyright fxsen <br/>
 * @date 2022/4/24 23:22 <br/>
 * @email <a href="mailto:fxsen@foxmail.com">
 * @author fxsen
 */
package com.fxsen.platform.web.controllers;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fxsen.commons.core.annotation.RecordLog;
import com.fxsen.commons.core.annotation.RequiredPermission;
import com.fxsen.commons.core.base.result.ResponseResult;
import com.fxsen.commons.sysmanager.core.entity.Dictionary;
import com.fxsen.commons.sysmanager.core.entity.DictionaryCategory;
import com.fxsen.commons.sysmanager.core.entity.vo.DictionaryCategoryVo;
import com.fxsen.commons.sysmanager.core.service.IDictionaryCategoryService;
import com.fxsen.commons.sysmanager.core.service.IDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author fxsen
 * @date 2022/4/24 23:22
 * @email <a href="mailto:fxsen@foxmail.com">
 */
@RestController
@RequestMapping("/dictionaryCategory")
@RequiredPermission("dictionaryCategory")
@Api(tags = "系统：字典类别接口")
public class DictionaryCategoryController {
    @Autowired
    private IDictionaryCategoryService dictionaryCategoryService;
    @Autowired
    private IDictionaryService dictionaryService;
    @ApiOperation(value = "新增")
    @PostMapping("save")
    @RequiredPermission("dictionaryCategory:save")
    @RecordLog("新增字典类别")
    public ResponseResult save(@RequestBody DictionaryCategory dictionaryCategory){
        try {
            if (dictionaryCategory == null) {
                return ResponseResult.failed("未传请求参数！");
            }
            String name = dictionaryCategory.getName();
            if (StringUtils.isBlank(name)){
                return ResponseResult.failed("类别名称不能为空！");
            }
            if (dictionaryCategoryService.isExist("name",name)){
                return ResponseResult.failed("类别名称“"+name+"“已存在！");
            }
            boolean save = dictionaryCategoryService.save(dictionaryCategory);
            return save ? ResponseResult.success() : ResponseResult.saveFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "更新")
    @PostMapping("update")
    @RequiredPermission("dictionaryCategory:update")
    @RecordLog("更新字典类别")
    public ResponseResult update(@RequestBody DictionaryCategory dictionaryCategory){
        try {
            if (dictionaryCategory == null || StringUtils.isBlank(dictionaryCategory.getId())) {
                return ResponseResult.failed("未传请求参数！");
            }
            String name = dictionaryCategory.getName();
            if (StringUtils.isBlank(name)){
                return ResponseResult.failed("类别名称不能为空！");
            }
            DictionaryCategory old = dictionaryCategoryService.getById(dictionaryCategory.getId());
            if (old == null) {
                return ResponseResult.failed("未查询到数据！");
            }
            if (!old.getName().equals(name) && dictionaryCategoryService.isExist("name",name)){
                return ResponseResult.failed("类别名称“"+name+"“已存在！");
            }
            boolean update = dictionaryCategoryService.updateById(dictionaryCategory);
            return update ? ResponseResult.success() : ResponseResult.updateFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "删除")
    @GetMapping("delete")
    @RequiredPermission("dictionaryCategory:delete")
    @RecordLog("删除字典类别")
    public ResponseResult delete(@ApiParam("类别主键id") @RequestParam("id") String id){
        try {
            boolean remove = dictionaryCategoryService.removeById(id);
            if(remove){
                //删除关联的字典
                List<Dictionary> dictionaryList = dictionaryService.getByCategoryId(id);
                if (CollectionUtils.isNotEmpty(dictionaryList)){
                    dictionaryService.removeBatchByIds(dictionaryList);
                }
                return ResponseResult.success();
            }
            return ResponseResult.deleteFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "列表")
    @GetMapping("list")
    //@RequiredPermission("dictionaryCategory:list")
    public ResponseResult list(){
        try {
            LambdaQueryWrapper<DictionaryCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByAsc(DictionaryCategory::getOrderIndex);
            List<DictionaryCategory> categoryList = dictionaryCategoryService.list(queryWrapper);
            List<Map<String,String>> result = new LinkedList<>();
            categoryList.forEach(dictionaryCategory -> {
                Map<String,String> map = new HashMap<>(16);
                map.put("id",dictionaryCategory.getId());
                map.put("name",dictionaryCategory.getName());
                result.add(map);
            });
            return ResponseResult.success(result);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "列表，包含所有字典")
    @GetMapping("list/all")
    @RequiredPermission("dictionaryCategory:listAll")
    public ResponseResult listAll(){
        try {
            List<DictionaryCategoryVo> dictionaryAll = dictionaryCategoryService.getDictionaryAll();
            return ResponseResult.success(dictionaryAll);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
}
